﻿using Microsoft.EntityFrameworkCore;
using ProgrammingEducationSystem.DbConfig;
using ProgrammingEducationSystem.Repository.IRepository;
using System.Runtime.CompilerServices;

namespace ProgrammingEducationSystem.Repository
{
    public class BaseRepository<Model> : IBaseRepository<Model> where Model : class
    {

        protected readonly AppDbCtx _appDbCtx;

        public BaseRepository(AppDbCtx appDbCtx)
        {
            _appDbCtx = appDbCtx;
        }

        public async Task AddAsync(Model model)
        {
            await _appDbCtx.Set<Model>().AddAsync(model);
        }

        public void Delete(Model model)
        {
            _appDbCtx.Set<Model>().Remove(model);
        }

        public async Task<Model?> FindAsync(Guid id)
        {
            return await _appDbCtx.Set<Model>().FindAsync(id);
        }

        public async Task<List<Model>?> FindAllAsync()
        {
            return await _appDbCtx.Set<Model>().ToListAsync();

        }

        public void Update(Model model)
        {
             _appDbCtx.Set<Model>().Update(model);   
        }

        public async Task<bool> SaveAsync()
        {
            return (await _appDbCtx.SaveChangesAsync()) > 0;
        }

        public void DeleteList(List<Model> model)
        {
            _appDbCtx.Set<Model>().RemoveRange(model);
        }
    }
}
